﻿using DeviceState;
using DeviceState.Model;
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.Serial;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace EquipmentServer
{
    class Program
    {
        public static DevState dev = new DevState();
        public static SerialData serial = new SerialData();
        private static object locker = new object();
        public static List<string> list = new List<string>();

        static void Main(string[] args)
        {
            string logPath = "/home/pi/SmartCommunity/publish/Log"; //获取到更目录下的logs文件夹          
            if (!Directory.Exists(logPath))
            {
                return;
            }
            RunCommand("chmod 777 " + logPath);
            DirectoryInfo folder_base = new DirectoryInfo(logPath);
            FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
            foreach (var file_txt in files_base)
            {
                //获取文件创建时间
                DateTime fileCreateTime = file_txt.CreationTime;
                //获取当前时间
                DateTime now = DateTime.Now.Date.AddDays(-29);
                if (fileCreateTime < now)
                {
                    try
                    {
                        File.Delete(file_txt.FullName);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }

        public static void RunCommand(string command)
        {
            Process.Start("sudo", command);
        }

        public static string GetLocalIp()
        {
            try
            {
                //本地计算机网络连接信息
                IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
                //获取本机所有网络连接
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

                if (nics == null || nics.Length < 1)
                {
                    return "";
                }
                foreach (NetworkInterface adapter in nics)
                {
                    var adapterName = adapter.Name;
                    if (adapterName == "eth0")
                    {
                        return adapter.GetIPProperties().UnicastAddresses[0].Address.ToString();
                    }
                }
                return "";

            }
            catch (Exception e)
            {
                return "";
            }
        }

        public static string GetMACIp()
        {
            //本地计算机网络连接信息
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            //获取本机所有网络连接
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            if (nics == null || nics.Length < 1)
            {
                return "";
            }

            var MACIp = "";
            foreach (NetworkInterface adapter in nics)
            {
                var adapterName = adapter.Name;

                var adapterDescription = adapter.Description;
                var NetworkInterfaceType = adapter.NetworkInterfaceType;
                if (adapterName == "eth0")
                {
                    PhysicalAddress address = adapter.GetPhysicalAddress();
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        MACIp += bytes[i].ToString("X2");

                        if (i != bytes.Length - 1)
                        {
                            MACIp += "-";
                        }
                    }
                }
            }

            return MACIp;
        }
    }
}
